/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shaman.dialog.selectfolder;

import java.io.File;
import java.io.FileFilter;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import shaman.dialog.util.ExtensionFileFilter;

/**
 *
 * @author shaman
 */
public class TreeFolderModel implements TreeModel {

    public class FSNode
    {    
        private File f = null;

        public FSNode(String Path)
        {
            f = new File(Path);
            if (!f.isDirectory())
                throw new UnsupportedOperationException();
        }
        
        public FSNode(File f)
        {
            this.f = f;
        }

        public FSNode getChild(int i)
        {
            if (f != null)
                return new FSNode(f.listFiles(getFileFilter())[i]);
            else
                return new FSNode(File.listRoots()[i]);
        }

        public int getChildCount()
        {
            if (f != null)
            {
                File[] files = f.listFiles(getFileFilter());
                if (files != null)
                    return files.length;
                else
                    return 0;
            }
            else
                return File.listRoots().length;
        }

        private FileFilter fileFilter = new FileFilter() {

            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        };

        private FileFilter getFileFilter()
        {
            //if (fileFilter == null)
            //    fileFilter = (FileFilter) new ExtensionFileFilter("", "");
            return fileFilter;
        }

        @Override
        public String toString() {
            if (f == null)
                return "Компьютер";
            else
                if (f.getName().isEmpty())
                    return f.getAbsolutePath();
                else
                    return f.getName();
        }

        public File getFile()
        {
            return f;
        }
    }

    private FSNode root = new FSNode((File)null);

    public Object getRoot() {
        return root;
    }

    public Object getChild(Object parent, int index) {
        return ((FSNode) parent).getChild(index);
    }

    public int getChildCount(Object parent) {
        return ((FSNode) parent).getChildCount();
    }

    public boolean isLeaf(Object node) {
        return false;
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIndexOfChild(Object parent, Object child) {
        FSNode parentNode = (FSNode) parent;
        FSNode childNode = (FSNode) child;
        for (int i = 0; i < parentNode.getChildCount(); i++)
            if (childNode.toString().equals(parentNode.getChild(i).toString()))
                return i;
        return -1;
    }

    public void addTreeModelListener(TreeModelListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void removeTreeModelListener(TreeModelListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
